package com.cathyjava.easyflows.work;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chenjing
 * @date 2021/5/8
 */
@FunctionalInterface
public interface WorkPredicate {
    Boolean apply(WorkReport workReport);

    public WorkPredicate ALWAYS_FALSE = workReport -> false;
    public WorkPredicate ALWAYS_TRUE = workReport -> true;

    //关注一下这里的实现
    class TimesWorkPredicate implements WorkPredicate {
        private Integer times;
        private AtomicInteger counter = new AtomicInteger();

        @Override
        public Boolean apply(WorkReport workReport) {
            return this.counter.incrementAndGet() != times;
        }

        private TimesWorkPredicate(Integer times) {
            this.times = times;
        }

        public static TimesWorkPredicate times(Integer n) {
            return new TimesWorkPredicate(n);
        }
    }
}
